package cu.ds.logicalsync.fytasrealm;

import cu.ds.logicalsync.fytasrealm.communication.NetworkService;
import cu.ds.logicalsync.fytasrealm.ui.MainWindow;
import cu.ds.logicalsync.fytasrealm.ui.PrototypeUI;

/**
 * Singleton class represent Fyta's Realm Application
 * 
 * @author r.vashira
 * @deprecated use FytasRealmDesktop instead
 */
public final class FytasRealm {
	/*** Member Variables ***/
	private static FytasRealm g_theInstance;
	//private PrototypeUI view;
	private View view;
	// TODO: Using Service Facade and Controller
	//private DiscoveryServiceProvider discoveryService;
	//private DiscoveryServiceMock discoveryServiceMock;
	private NetworkService netService;
	
	/**
	 * Singleton Constructor
	 * 
	 * Create a new instance of MainApplication and initializes it. 
	 * This starts the JXTA network if it was not already started
	 */
	private FytasRealm() {
		// TODO: only one instance can be active, using Utility to detect whether it is 
		// only one instance
		g_theInstance = this;
		
		// Registering View with Application
		//this.view = new PrototypeUI(g_theInstance);
		//this.view = new MainWindow(g_theInstance);
		
		// Initialize Clients Communication
		initialize();
	}
	
	/*** Properties ***/
	public static FytasRealm getTheInstance() {
		return g_theInstance;
	}
	
	/*** Methods ***/
	
	/**
	 * Initialize the singleton
	 */
	public static void startGame() {
		if(g_theInstance == null) {
			g_theInstance = new FytasRealm();
		}
	}
	
	public void leaveGame() {
		// Clean up used resources/service
		this.netService.stop();
	}

	/**
	 * Initialize Clients communication
	 */
	private void initialize() {
		// Get client credential for further communication reference
		String clientCredential = (String) this.view.openLoginDialog();
		
		// TODO: Use Log4J
		System.out.println("clientCredential = " + clientCredential);
		this.view.setClientName(clientCredential);
		
		// TODO: Using ServiceFacade
		/*this.netService = new NetworkService(clientCredential);
		this.netService.addObserver(this.view.getPeersPanel());
		this.netService.start();*/
		
		/*this.discoveryService = new DiscoveryServiceProvider(clientCredential);
		this.discoveryService.addObserver(this.view.getPeersPanel());
		this.discoveryService.start();*/
		
		// Test with Mock Services
		/*this.discoveryServiceMock = new DiscoveryServiceMock(clientCredential);
		this.discoveryServiceMock.addObserver(this.view.getPeersPanel());
		this.discoveryServiceMock.start();*/
	}
	
	private void createGame() {
		// TODO: impl
	}
	
	private void joinGame() {
		// TODO: impl 
	}
	
	/**
	 * Main entry
	 * @param args
	 */	
	/*public static void main(String[] args) {
		FytasRealm.startGame();
	}*/

}
